jQuery.noConflict();
var aeRequest;
var userArray = new Array();
function createResponseXML(textXML) {
    // code for IE
    if (window.ActiveXObject) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(textXML);
        return xmlDoc;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(textXML, "text/xml");
        return xmlDoc;
    }
}


function createXmlHttpRequestObject() {
    var ro;
    var browser = navigator.appName;
    // Need to determine IE7 and not do this.
    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

function getSessionByAjax(){
    AdminArray.splice(0,AdminArray.length);
    var url='http://localhost:8080/NLPP_SEM4/LoadDataServlet?type=getsession';
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type",
    "application/x-www-form-urlencoded");
    aeRequest.setRequestHeader("Content-length", 0);
    aeRequest.setRequestHeader("Connection", "close");
    aeRequest.onreadystatechange = function() {
        if (aeRequest.readyState == 4) {
            if (aeRequest.status == 200) {
                var response = createResponseXML(aeRequest.responseText);
                var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;	
                if(result == 'null'){
                   drawMenuTop();
                }else{
                    var type_user = response.getElementsByTagName("type")[0].firstChild.nodeValue;
                    if(type_user == 'admin'){
                        window.href.location='';
                    }else{
                          var name = response.getElementsByTagName("username")[0].firstChild.nodeValue;
                          var id = response.getElementsByTagName("id")[0].firstChild.nodeValue;
                          userArray.push(id,name,type_user);
                          drawMenuTopAfterLogin(userArray);
                    }
                }
            }
        }
    };
    aeRequest.send(null);
}
function doLogin(username,password){
    userArray.splice(0,userArray.length);
    var url = "http://localhost:8080/NLPP_SEM4/LoginServlet?action=login";
    url+='&username='+username+'&password='+password;//url da danh deo dau ma co thang ranh con
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type",
    "application/x-www-form-urlencoded");
    aeRequest.setRequestHeader("Content-length", 0);
    aeRequest.setRequestHeader("Connection", "close");
    aeRequest.onreadystatechange = function() {
        if (aeRequest.readyState == 4) {
            if (aeRequest.status == 200) {
                var response = createResponseXML(aeRequest.responseText);
                var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;	
                if(result == 'error' || result == 'you need to active your account by email we sent you'){
                   jQuery("#lblInfor").text('Wrong username or password');
                }else{                   
                    var type_user = response.getElementsByTagName("typeusername")[0].firstChild.nodeValue;            
                    if(type_user == 'ADMIN'){
                        document.location.href="http://localhost:8080/NLPP_SEM4/managerconduct.jsp";
                       //alert("move");
                    }else{
                          var name = response.getElementsByTagName("username")[0].firstChild.nodeValue;
                          var id = response.getElementsByTagName("userid")[0].firstChild.nodeValue;//id ji day nhi?id cua thang login vao chu j
                          userArray.push(id,name,type_user);
                          drawMenuTopAfterLogin(userArray);
                          setVisibleLoginForm(false);
                         // alert("ko move");
                    }
                }
            }
        }
    };
    aeRequest.send(null);
}

function doRegister(username,password,email){
    var url=''+'?type=register&username='+username+'&password='+password+'&email='+email;
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type",
    "application/x-www-form-urlencoded");
    aeRequest.setRequestHeader("Content-length", 0);
    aeRequest.setRequestHeader("Connection", "close");
    aeRequest.onreadystatechange = function() {
        if (aeRequest.readyState == 4) {
            if (aeRequest.status == 200) {
                var response = createResponseXML(aeRequest.responseText);
                var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;	
                if(result == 'error'){
                  jQuery("#registerS").html("<label style=\"color:red;font-size:25px;\">Register Fail for server</label>");
                }else{
                    jQuery("#registerS").html("<label style=\"color:red;font-size:25px;\">Register Succsess</label>");
                     clearDataRegisterForm();
                }
            }
        }
    };
    aeRequest.send(null);
}


function submitLogin(){
    
       var username = jQuery("#txtUsername").val();
       var password = jQuery("#txtPass").val();   
       if(validateLoginForm(username, password)){
           doLogin(username, password);          
       }
       
}
function registerForm(){
    
    var email = jQuery("#txtEmail").val();
    var name = jQuery("#txtname").val();
    var pass = jQuery("#txtPassword").val();
    if(validateRegisterForm(name, pass, email)){
        doRegister(username, password, email);
    }
    
}





